Prerequisite

#clearing the environment
rm(list=ls())
ls()
## character(0)
#setting working directory
getwd()
## [1] "/Users/arifpras/OneDrive - The University of Nottingham/BB_KelasData/io-DataWrangling"
setwd("/Users/arifpras/OneDrive - The University of Nottingham/BB_KelasData/KelasData")
#dir()
#sources:
##--https://www.kaggle.com/michau96
##--https://www.kaggle.com/aditya2803
##--https://www.opfanpage.com/2018/06/29/one-piece-power-ranking-chart/
##--https://listfist.com/list-of-one-piece-characters-by-age

#".csv" files - local

#op_chapters <- read.csv(file.choose())
#op_chapters <- read.csv("/Users/arifpras/OneDrive - The University of Nottingham/BB_KelasData/KelasData/00_Datasets/OP_chapters.csv")

#op_characters <- read.csv(file.choose())
#op_characters <- read.csv("/Users/arifpras/OneDrive - The University of Nottingham/BB_KelasData/KelasData/00_Datasets/OP_characters.csv")

#op_ratings <- read.csv(file.choose())
#op_ratings <- read.csv("/Users/arifpras/OneDrive - The University of Nottingham/BB_KelasData/KelasData/00_Datasets/OP_ratings.csv")

#op_powers <- read.csv(file.choose())
#op_powers <- read.csv("/Users/arifpras/OneDrive - The University of Nottingham/BB_KelasData/KelasData/00_Datasets/OP_powers.csv")

#op_ages <- read.csv(file.choose())
#op_ages <- read.csv("/Users/arifpras/OneDrive - The University of Nottingham/BB_KelasData/KelasData/00_Datasets/OP_ages.csv")

#".xlsx" files - local
#op_chapters <- read_excel(path = "/Users/arifpras/OneDrive - The University of Nottingham/BB_KelasData/KelasData/00_Datasets/OP_all.xlsx", sheet = "OP_chapters")
#op_characters <- read_excel(path = "/Users/arifpras/OneDrive - The University of Nottingham/BB_KelasData/KelasData/00_Datasets/OP_all.xlsx", sheet = "OP_characters")
#op_ratings <- read_excel(path = "/Users/arifpras/OneDrive - The University of Nottingham/BB_KelasData/KelasData/00_Datasets/OP_all.xlsx", sheet = "OP_ratings")
op_powers <- readxl::read_excel(path = "/Users/arifpras/OneDrive - The University of Nottingham/BB_KelasData/KelasData/00_Datasets/OP_all.xlsx", sheet = 4)
op_ages <- read_excel(path = "/Users/arifpras/OneDrive - The University of Nottingham/BB_KelasData/KelasData/00_Datasets/OP_all.xlsx", sheet = "OP_ages")

#".csv" files - cloud
#op_chapters <- read.csv("https://www.dropbox.com/s/t1vrxsa4ad0m2c4/OP_chapters.csv?dl=1")
#op_characters <- read.csv("https://www.dropbox.com/s/e74njw4iw7qfzbl/OP_characters.csv?dl=1")
#op_ratings <- read.csv("https://www.dropbox.com/s/qvdhlnkyk02giuj/OP_ratings.csv?dl=1")
#op_powers <- read.csv("https://www.dropbox.com/s/t3gbzzscz438ecp/OP_powers.csv?dl=1")
#op_ages <- read.csv("https://www.dropbox.com/s/r24vzv7eyz35irr/OP_ages.csv?dl=1")

#require(devtools)
#install_github("Displayr/flipAPI")
#library(flipAPI)

#".xlsx" files - cloud
#op_chapters <- flipAPI::DownloadXLSX("https://www.dropbox.com/s/5mev3xo0cd3tc61/OP_all.xlsx?dl=0", sheet = 1)
#op_characters <- flipAPI::DownloadXLSX("https://www.dropbox.com/s/5mev3xo0cd3tc61/OP_all.xlsx?dl=0", sheet = "OP_characters")
#op_ratings <- flipAPI::DownloadXLSX("https://www.dropbox.com/s/5mev3xo0cd3tc61/OP_all.xlsx?dl=0", sheet = 3)
#op_powers <- flipAPI::DownloadXLSX("https://www.dropbox.com/s/5mev3xo0cd3tc61/OP_all.xlsx?dl=0", sheet = "OP_powers", want.data.frame = TRUE)
#op_ages <- flipAPI::DownloadXLSX("https://www.dropbox.com/s/5mev3xo0cd3tc61/OP_all.xlsx?dl=0", sheet = "OP_ages", want.data.frame = TRUE)
str(op_powers)
## tibble [56 × 4] (S3: tbl_df/tbl/data.frame)
##  $ power       : num [1:56] 100 100 100 100 99 95 95 94.5 94 93 ...
##  $ character   : chr [1:56] "Red Haired Shanks" "Marshall D. Teach" "Charlotte Linlin" "Kaido" ...
##  $ level       : chr [1:56] "Yonko" "Yonko" "Yonko" "Yonko" ...
##  $ affiliations: chr [1:56] "Red Haired Pirates" "Blackbeard Pirates" "Big Mom Pirates" "Beasts Pirates" ...
str(op_ages)
## tibble [482 × 2] (S3: tbl_df/tbl/data.frame)
##  $ name: chr [1:482] "Zunesha" "Jarul" "Jorul" "Hatcha" ...
##  $ age : num [1:482] 1000 408 344 188 160 160 160 159 156 153 ...
glimpse(op_powers)
## Rows: 56
## Columns: 4
## $ power        <dbl> 100.0, 100.0, 100.0, 100.0, 99.0, 95.0, 95.0, 94.5, 94.0,…
## $ character    <chr> "Red Haired Shanks", "Marshall D. Teach", "Charlotte Linl…
## $ level        <chr> "Yonko", "Yonko", "Yonko", "Yonko", "Admiral", "Admiral",…
## $ affiliations <chr> "Red Haired Pirates", "Blackbeard Pirates", "Big Mom Pira…
glimpse(op_ages)
## Rows: 482
## Columns: 2
## $ name <chr> "Zunesha", "Jarul", "Jorul", "Hatcha", "Brogy", "Dorry", "Morley"…
## $ age  <dbl> 1000, 408, 344, 188, 160, 160, 160, 159, 156, 153, 141, 120, 105,…

Data Wrangling

op_powers %>% 
  dplyr::filter(affiliations == "Marine")
## # A tibble: 10 × 4
##    power character        level       affiliations
##    <dbl> <chr>            <chr>       <chr>       
##  1  95   Sakazuki Akainu  Admiral     Marine      
##  2  95   Monkey D. Garp   Admiral     Marine      
##  3  94.5 Sengoku          Admiral     Marine      
##  4  94   Kuzan Aokiji     Admiral     Marine      
##  5  93   Borsalino Kizaru Admiral     Marine      
##  6  92   Issho Fujitora   Admiral     Marine      
##  7  85   Magellan         Shichibukai Marine      
##  8  80.5 Bartholomew Kuma Shichibukai Marine      
##  9  74   Rob Lucci        General     Marine      
## 10  72.5 Smoker           General     Marine
op_powers %>%
  arrange(desc(character))
## # A tibble: 56 × 4
##    power character              level       affiliations       
##    <dbl> <chr>                  <chr>       <chr>              
##  1  74   X Drake                General     Drake Pirates      
##  2  75.5 Vinsmoke Sanji         Shichibukai Straw Hat Pirates  
##  3  64   Usopp                  General     Straw Hat Pirates  
##  4  77   Urouge                 Shichibukai Fallen Monk Pirates
##  5  79.5 Trafalgar D. Water Law Shichibukai Heart Pirates      
##  6  72.5 Smoker                 General     Marine             
##  7  93   Silvers Rayleigh       Admiral     Roger Pirates      
##  8  85   Shiryu of the Rain     Shichibukai Blackbeard Pirates 
##  9  94.5 Sengoku                Admiral     Marine             
## 10  73   Scratchmen Apoo        General     On Air Pirates     
## # … with 46 more rows
law <- op_powers %>% 
  mutate(character02 = 
           recode(character, 
                  "Trafalgar D. Water Law" = "Trafalgar D. Law"))
law
## # A tibble: 56 × 5
##    power character         level   affiliations       character02      
##    <dbl> <chr>             <chr>   <chr>              <chr>            
##  1 100   Red Haired Shanks Yonko   Red Haired Pirates Red Haired Shanks
##  2 100   Marshall D. Teach Yonko   Blackbeard Pirates Marshall D. Teach
##  3 100   Charlotte Linlin  Yonko   Big Mom Pirates    Charlotte Linlin 
##  4 100   Kaido             Yonko   Beasts Pirates     Kaido            
##  5  99   Monkey D. Dragon  Admiral Revolutionary Army Monkey D. Dragon 
##  6  95   Sakazuki Akainu   Admiral Marine             Sakazuki Akainu  
##  7  95   Monkey D. Garp    Admiral Marine             Monkey D. Garp   
##  8  94.5 Sengoku           Admiral Marine             Sengoku          
##  9  94   Kuzan Aokiji      Admiral Marine             Kuzan Aokiji     
## 10  93   Borsalino Kizaru  Admiral Marine             Borsalino Kizaru 
## # … with 46 more rows
op_powers %>% 
  slice(56L)
## # A tibble: 1 × 4
##   power character level   affiliations           
##   <dbl> <chr>     <chr>   <chr>                  
## 1    60 Orlumbus  General Yonta Maria Grand Fleet
op_powers %>% 
  slice(n())
## # A tibble: 1 × 4
##   power character level   affiliations           
##   <dbl> <chr>     <chr>   <chr>                  
## 1    60 Orlumbus  General Yonta Maria Grand Fleet
op_powers %>% 
  slice(50:n())
## # A tibble: 7 × 4
##   power character  level   affiliations             
##   <dbl> <chr>      <chr>   <chr>                    
## 1    66 Brook      General Straw Hat Pirates        
## 2    65 Hajrudin   General New Giant Warrior Pirates
## 3    65 Ideo       General Ideo Pirates             
## 4    65 Nico Robin General Straw Hat Pirates        
## 5    65 Buggy      General Buggy Pirates            
## 6    64 Usopp      General Straw Hat Pirates        
## 7    60 Orlumbus   General Yonta Maria Grand Fleet
op_powers %>% 
  slice_min(character, n = 5, with_ties = FALSE)
## # A tibble: 5 × 4
##   power character        level       affiliations   
##   <dbl> <chr>            <chr>       <chr>          
## 1  80.5 Bartholomew Kuma Shichibukai Marine         
## 2  72   Bartolomeo       General     Barto Club     
## 3  72   Basil Hawkins    General     Hawkins Pirates
## 4  76   Boa Hancock      Shichibukai Kuja Pirates   
## 5  93   Borsalino Kizaru Admiral     Marine
op_powers %>% 
  slice_min(character, prop = 0.5, with_ties = TRUE)
## # A tibble: 28 × 4
##    power character        level       affiliations     
##    <dbl> <chr>            <chr>       <chr>            
##  1  80.5 Bartholomew Kuma Shichibukai Marine           
##  2  72   Bartolomeo       General     Barto Club       
##  3  72   Basil Hawkins    General     Hawkins Pirates  
##  4  76   Boa Hancock      Shichibukai Kuja Pirates     
##  5  93   Borsalino Kizaru Admiral     Marine           
##  6  66   Brook            General     Straw Hat Pirates
##  7  65   Buggy            General     Buggy Pirates    
##  8  71   Caesar Clown     General     Fire Tank Pirates
##  9  74   Capone Bege      General     Fire Tank Pirates
## 10  72.5 Cavendish        General     Beautiful Pirates
## # … with 18 more rows
op_powers %>% 
  slice_tail(n = 5)
## # A tibble: 5 × 4
##   power character  level   affiliations           
##   <dbl> <chr>      <chr>   <chr>                  
## 1    65 Ideo       General Ideo Pirates           
## 2    65 Nico Robin General Straw Hat Pirates      
## 3    65 Buggy      General Buggy Pirates          
## 4    64 Usopp      General Straw Hat Pirates      
## 5    60 Orlumbus   General Yonta Maria Grand Fleet
op_powers %>% 
  slice_tail(prop = 0.5)
## # A tibble: 28 × 4
##    power character       level       affiliations         
##    <dbl> <chr>           <chr>       <chr>                
##  1  77   Jesus Burgess   Shichibukai Blackbeard Pirates   
##  2  77   Urouge          Shichibukai Fallen Monk Pirates  
##  3  77   Eustass Kid     Shichibukai Kid Pirates          
##  4  76.5 Crocodile       Shichibukai Sichibukai           
##  5  76   Emporio Ivankov Shichibukai Revolutionary Army   
##  6  76   Boa Hancock     Shichibukai Kuja Pirates         
##  7  75.5 Vinsmoke Sanji  Shichibukai Straw Hat Pirates    
##  8  75   Gecko Moria     Shichibukai Thriller Bark Pirates
##  9  74   Rob Lucci       General     Marine               
## 10  74   Capone Bege     General     Fire Tank Pirates    
## # … with 18 more rows
op_powers %>% 
  slice_max(affiliations, n = 5, with_ties = FALSE)
## # A tibble: 5 × 4
##   power character    level       affiliations           
##   <dbl> <chr>        <chr>       <chr>                  
## 1  60   Orlumbus     General     Yonta Maria Grand Fleet
## 2  89.5 Marco        Shichibukai Whitebeard Pirates     
## 3  84   Diamond Jozu Shichibukai Whitebeard Pirates     
## 4  68   Leo          General     Tontatta Pirates       
## 5  75   Gecko Moria  Shichibukai Thriller Bark Pirates
op_powers %>% 
  slice_max(affiliations, prop = 0.5, with_ties = TRUE)
## # A tibble: 33 × 4
##    power character       level       affiliations           
##    <dbl> <chr>           <chr>       <chr>                  
##  1  60   Orlumbus        General     Yonta Maria Grand Fleet
##  2  89.5 Marco           Shichibukai Whitebeard Pirates     
##  3  84   Diamond Jozu    Shichibukai Whitebeard Pirates     
##  4  68   Leo             General     Tontatta Pirates       
##  5  75   Gecko Moria     Shichibukai Thriller Bark Pirates  
##  6  85   Monkey D. Luffy Shichibukai Straw Hat Pirates      
##  7  79   Jinbe           Shichibukai Straw Hat Pirates      
##  8  79   Roronoa Zoro    Shichibukai Straw Hat Pirates      
##  9  75.5 Vinsmoke Sanji  Shichibukai Straw Hat Pirates      
## 10  70   Franky          General     Straw Hat Pirates      
## # … with 23 more rows
op_powers %>% 
  slice_head(n = 5)
## # A tibble: 5 × 4
##   power character         level   affiliations      
##   <dbl> <chr>             <chr>   <chr>             
## 1   100 Red Haired Shanks Yonko   Red Haired Pirates
## 2   100 Marshall D. Teach Yonko   Blackbeard Pirates
## 3   100 Charlotte Linlin  Yonko   Big Mom Pirates   
## 4   100 Kaido             Yonko   Beasts Pirates    
## 5    99 Monkey D. Dragon  Admiral Revolutionary Army
op_powers %>% 
  slice_head(prop = 0.5)
## # A tibble: 28 × 4
##    power character         level   affiliations      
##    <dbl> <chr>             <chr>   <chr>             
##  1 100   Red Haired Shanks Yonko   Red Haired Pirates
##  2 100   Marshall D. Teach Yonko   Blackbeard Pirates
##  3 100   Charlotte Linlin  Yonko   Big Mom Pirates   
##  4 100   Kaido             Yonko   Beasts Pirates    
##  5  99   Monkey D. Dragon  Admiral Revolutionary Army
##  6  95   Sakazuki Akainu   Admiral Marine            
##  7  95   Monkey D. Garp    Admiral Marine            
##  8  94.5 Sengoku           Admiral Marine            
##  9  94   Kuzan Aokiji      Admiral Marine            
## 10  93   Borsalino Kizaru  Admiral Marine            
## # … with 18 more rows
db03 <- op_powers %>%
  select(character, affiliations)
db03
## # A tibble: 56 × 2
##    character         affiliations      
##    <chr>             <chr>             
##  1 Red Haired Shanks Red Haired Pirates
##  2 Marshall D. Teach Blackbeard Pirates
##  3 Charlotte Linlin  Big Mom Pirates   
##  4 Kaido             Beasts Pirates    
##  5 Monkey D. Dragon  Revolutionary Army
##  6 Sakazuki Akainu   Marine            
##  7 Monkey D. Garp    Marine            
##  8 Sengoku           Marine            
##  9 Kuzan Aokiji      Marine            
## 10 Borsalino Kizaru  Marine            
## # … with 46 more rows
db04 <- op_powers %>% 
  select(-level)
db04
## # A tibble: 56 × 3
##    power character         affiliations      
##    <dbl> <chr>             <chr>             
##  1 100   Red Haired Shanks Red Haired Pirates
##  2 100   Marshall D. Teach Blackbeard Pirates
##  3 100   Charlotte Linlin  Big Mom Pirates   
##  4 100   Kaido             Beasts Pirates    
##  5  99   Monkey D. Dragon  Revolutionary Army
##  6  95   Sakazuki Akainu   Marine            
##  7  95   Monkey D. Garp    Marine            
##  8  94.5 Sengoku           Marine            
##  9  94   Kuzan Aokiji      Marine            
## 10  93   Borsalino Kizaru  Marine            
## # … with 46 more rows
db05 <- op_powers %>% 
  relocate(character, power, affiliations)
db05
## # A tibble: 56 × 4
##    character         power affiliations       level  
##    <chr>             <dbl> <chr>              <chr>  
##  1 Red Haired Shanks 100   Red Haired Pirates Yonko  
##  2 Marshall D. Teach 100   Blackbeard Pirates Yonko  
##  3 Charlotte Linlin  100   Big Mom Pirates    Yonko  
##  4 Kaido             100   Beasts Pirates     Yonko  
##  5 Monkey D. Dragon   99   Revolutionary Army Admiral
##  6 Sakazuki Akainu    95   Marine             Admiral
##  7 Monkey D. Garp     95   Marine             Admiral
##  8 Sengoku            94.5 Marine             Admiral
##  9 Kuzan Aokiji       94   Marine             Admiral
## 10 Borsalino Kizaru   93   Marine             Admiral
## # … with 46 more rows
status <- op_powers %>% 
  rename("status" = "level")
status
## # A tibble: 56 × 4
##    power character         status  affiliations      
##    <dbl> <chr>             <chr>   <chr>             
##  1 100   Red Haired Shanks Yonko   Red Haired Pirates
##  2 100   Marshall D. Teach Yonko   Blackbeard Pirates
##  3 100   Charlotte Linlin  Yonko   Big Mom Pirates   
##  4 100   Kaido             Yonko   Beasts Pirates    
##  5  99   Monkey D. Dragon  Admiral Revolutionary Army
##  6  95   Sakazuki Akainu   Admiral Marine            
##  7  95   Monkey D. Garp    Admiral Marine            
##  8  94.5 Sengoku           Admiral Marine            
##  9  94   Kuzan Aokiji      Admiral Marine            
## 10  93   Borsalino Kizaru  Admiral Marine            
## # … with 46 more rows
op_powers %>% 
  filter(affiliations == "Marine") %>% 
  count()
## # A tibble: 1 × 1
##       n
##   <int>
## 1    10
db06 <- op_powers %>% 
  mutate(anime = "One Piece",
         power_2 = power/2)
db06
## # A tibble: 56 × 6
##    power character         level   affiliations       anime     power_2
##    <dbl> <chr>             <chr>   <chr>              <chr>       <dbl>
##  1 100   Red Haired Shanks Yonko   Red Haired Pirates One Piece    50  
##  2 100   Marshall D. Teach Yonko   Blackbeard Pirates One Piece    50  
##  3 100   Charlotte Linlin  Yonko   Big Mom Pirates    One Piece    50  
##  4 100   Kaido             Yonko   Beasts Pirates     One Piece    50  
##  5  99   Monkey D. Dragon  Admiral Revolutionary Army One Piece    49.5
##  6  95   Sakazuki Akainu   Admiral Marine             One Piece    47.5
##  7  95   Monkey D. Garp    Admiral Marine             One Piece    47.5
##  8  94.5 Sengoku           Admiral Marine             One Piece    47.2
##  9  94   Kuzan Aokiji      Admiral Marine             One Piece    47  
## 10  93   Borsalino Kizaru  Admiral Marine             One Piece    46.5
## # … with 46 more rows
db07 <- op_powers %>% 
  transmute(anime = "One Piece",
            power_2 = power/2)
db07
## # A tibble: 56 × 2
##    anime     power_2
##    <chr>       <dbl>
##  1 One Piece    50  
##  2 One Piece    50  
##  3 One Piece    50  
##  4 One Piece    50  
##  5 One Piece    49.5
##  6 One Piece    47.5
##  7 One Piece    47.5
##  8 One Piece    47.2
##  9 One Piece    47  
## 10 One Piece    46.5
## # … with 46 more rows
db08 <- op_powers %>% 
  arrange(affiliations, desc(power)) %>% 
  group_by(affiliations) %>% 
  mutate(rank = row_number()) %>% 
  ungroup() %>% #close the group_by()
  arrange(affiliations) %>% 
  select(character, affiliations, rank)
db08
## # A tibble: 56 × 3
##    character          affiliations        rank
##    <chr>              <chr>              <int>
##  1 Bartolomeo         Barto Club             1
##  2 Kaido              Beasts Pirates         1
##  3 Cavendish          Beautiful Pirates      1
##  4 Charlotte Linlin   Big Mom Pirates        1
##  5 Charlotte Katakuri Big Mom Pirates        2
##  6 Charlotte Smoothie Big Mom Pirates        3
##  7 Charlotte Cracker  Big Mom Pirates        4
##  8 Marshall D. Teach  Blackbeard Pirates     1
##  9 Shiryu of the Rain Blackbeard Pirates     2
## 10 Jesus Burgess      Blackbeard Pirates     3
## # … with 46 more rows
db09 <- op_powers %>% 
  arrange(affiliations, desc(power)) %>% 
  group_by(affiliations) %>% 
  summarise(avg = mean(power, na.rm = TRUE),
            n = n()) %>%
  ungroup()
db09
## # A tibble: 30 × 3
##    affiliations          avg     n
##    <chr>               <dbl> <int>
##  1 Barto Club           72       1
##  2 Beasts Pirates      100       1
##  3 Beautiful Pirates    72.5     1
##  4 Big Mom Pirates      86.4     4
##  5 Blackbeard Pirates   87.3     3
##  6 Buggy Pirates        65       1
##  7 Chinjao Family       69       1
##  8 Donquixote Pirates   81       1
##  9 Drake Pirates        74       1
## 10 Fallen Monk Pirates  77       1
## # … with 20 more rows
villain_aff <- data.frame(
  character = c("Marshall D. Teach", "Charlotte Linlin", "Kaido", "Sakazuki Akainu", "Borsalino Kizaru"),
  affiliation = c("Blackbeard Pirates", "Big Mom Pirates", "Beasts Pirates", "Marine", "Marine")
)

character_power <- data.frame(
  name = c("Marshall D. Teach", "Kaido", "Sakazuki Akainu", "Monkey D. Dragon", "Silvers Rayleigh"),
  power = c(100, 100, 95, 99, 93)
)
db11 <- op_powers %>% 
  mutate(villain = ifelse(affiliations == "Blackbeard Pirates", "Villain", "NA"))
db11
## # A tibble: 56 × 5
##    power character         level   affiliations       villain
##    <dbl> <chr>             <chr>   <chr>              <chr>  
##  1 100   Red Haired Shanks Yonko   Red Haired Pirates NA     
##  2 100   Marshall D. Teach Yonko   Blackbeard Pirates Villain
##  3 100   Charlotte Linlin  Yonko   Big Mom Pirates    NA     
##  4 100   Kaido             Yonko   Beasts Pirates     NA     
##  5  99   Monkey D. Dragon  Admiral Revolutionary Army NA     
##  6  95   Sakazuki Akainu   Admiral Marine             NA     
##  7  95   Monkey D. Garp    Admiral Marine             NA     
##  8  94.5 Sengoku           Admiral Marine             NA     
##  9  94   Kuzan Aokiji      Admiral Marine             NA     
## 10  93   Borsalino Kizaru  Admiral Marine             NA     
## # … with 46 more rows
luffy00 <- tribble(
  ~arc, ~power,
  "1-Punk Hazard", 75,
  "2-Dressrosa", NA,
  "3-Zou", 80,
  "4-Whole Cake Island", 82,
  "5-Levely", NA
)
luffy00
## # A tibble: 5 × 2
##   arc                 power
##   <chr>               <dbl>
## 1 1-Punk Hazard          75
## 2 2-Dressrosa            NA
## 3 3-Zou                  80
## 4 4-Whole Cake Island    82
## 5 5-Levely               NA
luffy00 <- luffy00 %>% 
  add_row(arc = "6- Wano Country",
          power = 85)
luffy00
## # A tibble: 6 × 2
##   arc                 power
##   <chr>               <dbl>
## 1 1-Punk Hazard          75
## 2 2-Dressrosa            NA
## 3 3-Zou                  80
## 4 4-Whole Cake Island    82
## 5 5-Levely               NA
## 6 6- Wano Country        85
is.na(luffy00)
##        arc power
## [1,] FALSE FALSE
## [2,] FALSE  TRUE
## [3,] FALSE FALSE
## [4,] FALSE FALSE
## [5,] FALSE  TRUE
## [6,] FALSE FALSE
#data frame
colSums(is.na(luffy00))
##   arc power 
##     0     2
mean(luffy00$power)
## [1] NA
mean(luffy00$power, na.rm = TRUE)
## [1] 80.5
#vector
which(is.na(luffy00))
## [1]  8 11
sum(is.na(luffy00)) 
## [1] 2
db01a <-  op_powers %>%
  left_join(op_ages, by = c("character" = "name")) %>% 
  select(-level) %>% 
  relocate(character, affiliations, power, age)
db01a
## # A tibble: 56 × 4
##    character         affiliations       power   age
##    <chr>             <chr>              <dbl> <dbl>
##  1 Red Haired Shanks Red Haired Pirates 100      39
##  2 Marshall D. Teach Blackbeard Pirates 100      40
##  3 Charlotte Linlin  Big Mom Pirates    100      68
##  4 Kaido             Beasts Pirates     100      59
##  5 Monkey D. Dragon  Revolutionary Army  99      55
##  6 Sakazuki Akainu   Marine              95      55
##  7 Monkey D. Garp    Marine              95      78
##  8 Sengoku           Marine              94.5    79
##  9 Kuzan Aokiji      Marine              94      49
## 10 Borsalino Kizaru  Marine              93      58
## # … with 46 more rows
db01b <- db01a %>% 
  pivot_longer(cols = power:age, 
               names_to = "variable", 
               values_to = "value")
db01b
## # A tibble: 112 × 4
##    character         affiliations       variable value
##    <chr>             <chr>              <chr>    <dbl>
##  1 Red Haired Shanks Red Haired Pirates power      100
##  2 Red Haired Shanks Red Haired Pirates age         39
##  3 Marshall D. Teach Blackbeard Pirates power      100
##  4 Marshall D. Teach Blackbeard Pirates age         40
##  5 Charlotte Linlin  Big Mom Pirates    power      100
##  6 Charlotte Linlin  Big Mom Pirates    age         68
##  7 Kaido             Beasts Pirates     power      100
##  8 Kaido             Beasts Pirates     age         59
##  9 Monkey D. Dragon  Revolutionary Army power       99
## 10 Monkey D. Dragon  Revolutionary Army age         55
## # … with 102 more rows

Data Visualisation

plot01a <- op_powers %>%
  left_join(op_ages, by = c("character" = "name")) %>%
  relocate(character, age, power) %>%
  #group_by(level) %>%
  slice_max(power, n = 15, with_ties = FALSE) %>%
  #ungroup() %>%
  ggplot(aes(x = reorder(character, power),
             y = age)) +
  geom_point(aes(color = factor(affiliations), size = power)) +
  coord_cartesian(expand = TRUE, clip = "on") +
  coord_flip() +
  nord::scale_color_nord("aurora") +
  xlab("") +
  ylab("Age") +
  theme_test() +
  theme(
    axis.text.x = element_text(size = 8),
    axis.ticks.x = element_blank(),
    axis.line.x = element_blank(),
    axis.title.x = element_text(size = 7),
    axis.line.y = element_blank(),
    axis.title.y = element_text(size = 7),
    axis.text.y = element_text(size = 8),
    plot.title.position = "plot",
    plot.caption.position = "plot",
    legend.title = element_text(size = 7),
    legend.text = element_text(size = 7),
    legend.key.size = unit(0.5, "cm"),
    legend.spacing = unit(1, "cm"),
    plot.title = element_text(hjust = 0, size = 13, face = "bold"),
    plot.subtitle = element_text(hjust = 0, size = 12),
    plot.caption = element_text(hjust = 0, size = 12),
    # legend.title = element_blank(),
    legend.position = "right",
    strip.text.x = element_text(size = 8),
    panel.grid.major.y = element_line(colour = "grey97"),
    panel.ontop = FALSE,
    panel.spacing = unit(1, "lines")
  ) +
  guides(size = guide_legend("Power"), color = guide_legend("Affiliation")) +
  labs(title = "The top 15-most powerful characters",
       subtitle = "",
       caption = "") +
  scale_size_continuous(range = c(1, 5)) +
  scale_y_continuous(limits = c(10, 90),
                     breaks = c(20, 50, 80))
## Coordinate system already present. Adding new coordinate system, which will replace the existing one.
plot01a

ggsave(
  filename = "plot01a.png",
  plot = plot01a,
  #device = cairo_pdf,
  width = 297,
  height = 210,
  units = "mm"
)
plot01b <- op_powers %>%
  left_join(op_ages, by = c("character" = "name")) %>%
  relocate(character, age, power) %>%
  #group_by(level) %>%
  slice_max(power, n = 15, with_ties = FALSE) %>%
  #ungroup() %>%
  ggplot(aes(x = reorder(character, power),
             y = age)) +
  geom_point(aes(color = factor(affiliations), size = power)) +
  coord_cartesian(expand = TRUE, clip = "on") +
  coord_flip() +
  nord::scale_color_nord("aurora") +
  xlab("") +
  ylab("Age") +
  theme_test() +
  theme(
    axis.text.x = element_text(size = 8),
    axis.ticks.x = element_blank(),
    axis.line.x = element_blank(),
    axis.title.x = element_text(size = 7),
    axis.line.y = element_blank(),
    axis.title.y = element_text(size = 7),
    axis.text.y = element_text(size = 8),
    plot.title.position = "plot",
    plot.caption.position = "plot",
    legend.title = element_text(size = 7),
    legend.text = element_text(size = 7),
    legend.key.size = unit(0.5, "cm"),
    legend.spacing = unit(1, "cm"),
    plot.title = element_text(hjust = 0, size = 13, face = "bold"),
    plot.subtitle = element_text(hjust = 0, size = 12),
    plot.caption = element_text(hjust = 0, size = 12),
    # legend.title = element_blank(),
    legend.position = "right",
    strip.text.x = element_text(size = 8),
    panel.grid.major.y = element_line(colour = "grey97"),
    panel.ontop = FALSE,
    panel.spacing = unit(1, "lines")
  ) +
  guides(size = guide_legend("Power"), color = guide_legend("Affiliation")) +
  labs(title = "The top 15-most powerful characters",
       subtitle = "",
       caption = "") +
  scale_size_continuous(range = c(1, 5)) +
  scale_y_continuous(limits = c(10, 90),
                     breaks = c(20, 50, 80)) +
  facet_grid(. ~ level)
## Coordinate system already present. Adding new coordinate system, which will replace the existing one.
plot01b

plot02a <- op_powers %>%
  left_join(op_ages, by = c("character" = "name")) %>%
  relocate(character, age, power) %>%
  #group_by(level) %>%
  slice_max(power, n = 15, with_ties = FALSE) %>%
  #ungroup() %>%
  mutate(text = paste(
    "Character: ", reorder(character, power), 
    "\nAffiliation: ", factor(affiliations), 
    "\nAge: ", age, 
    "\nPower: ", power, 
    sep = "")) %>%
  ggplot(aes(x = reorder(character, power),
             y = factor(affiliations),
             text = text)) +
  geom_point(aes(color = age, size = power)) +
  coord_cartesian(expand = TRUE, clip = "on") +
  coord_flip() +
  #nord::scale_color_nord("victory_bonds", discrete = FALSE) +
  viridis::scale_colour_viridis(option = "viridis", direction = -1) +
  xlab("") +
  ylab("") +
  theme_test() +
  theme(
    axis.text.x = element_text(
      size = 8,
      angle = 90,
      vjust = 0.5,
      hjust = 1
    ),
    axis.ticks.x = element_blank(),
    axis.line.x = element_blank(),
    axis.title.x = element_text(size = 7),
    axis.line.y = element_blank(),
    axis.title.y = element_text(size = 7),
    axis.text.y = element_text(size = 8),
    plot.title.position = "plot",
    plot.caption.position = "plot",
    legend.title = element_text(size = 7),
    legend.text = element_text(size = 7),
    legend.key.size = unit(0.5, "cm"),
    legend.spacing = unit(1, "cm"),
    plot.title = element_text(hjust = 0, size = 13, face = "bold"),
    plot.subtitle = element_text(hjust = 0, size = 12),
    plot.caption = element_text(hjust = 0, size = 12),
    # legend.title = element_blank(),
    legend.position = "none",
    strip.text.x = element_text(size = 8),
    panel.grid.major.y = element_line(colour = "grey97"),
    panel.ontop = FALSE
  ) +
  guides(size = guide_legend("Power"), color = guide_colorbar("Age")) +
  labs(title = "",
       subtitle = "",
       caption = "") +
  scale_size_continuous(range = c(1, 5))
## Coordinate system already present. Adding new coordinate system, which will replace the existing one.
ggplotly(plot02a, tooltip = "text")

Figure 3: The top 15-most powerful characters, without facet_grid(. ~ level)

plot02b <- op_powers %>% 
  left_join(op_ages, by = c("character" = "name")) %>% 
  relocate(character, age, power) %>% 
  #group_by(level) %>% 
  slice_max(power, n = 15, with_ties = FALSE) %>%
  #ungroup() %>% 
  mutate(text = paste(
    "Character: ", reorder(character, power), 
    "\nAffiliation: ", factor(affiliations), 
    "\nAge: ", age, 
    "\nPower: ", power, 
    sep = "")) %>%
  ggplot(aes(x = reorder(character, power), 
             y = factor(affiliations),
             text = text)) +
  geom_point(aes(color = age, size = power)) +
  coord_cartesian(expand = TRUE, clip = "on") +
  coord_flip() +
  #nord::scale_color_nord("victory_bonds", discrete = FALSE) +
  viridis::scale_colour_viridis(option = "viridis", direction = -1) +
  xlab("") +
  ylab("") +
  theme_test() +
  theme(
    axis.text.x = element_text(size = 8, angle = 90, vjust = 0.5, hjust=1),
    axis.ticks.x = element_blank(),
    axis.line.x = element_blank(),
    axis.title.x = element_text(size = 7),
    axis.line.y = element_blank(),
    axis.title.y = element_text(size = 7),
    axis.text.y = element_text(size = 8),
    plot.title.position = "plot",
    plot.caption.position = "plot",
    legend.title = element_text(size = 7),
    legend.text = element_text(size = 7),
    legend.key.size = unit(0.5, "cm"),
    legend.spacing = unit(1, "cm"),
    plot.title = element_text(
      hjust = 0, size = 13, face = "bold"
    ),
    plot.subtitle = element_text(hjust = 0, size = 12),
    plot.caption = element_text(hjust = 0, size = 12),
    # legend.title = element_blank(),
    legend.position = "none",
    strip.text.x = element_text(size = 8),
    panel.grid.major.y = element_line(colour = "grey97"),
    panel.ontop = FALSE,
    panel.spacing = unit(1, "lines")
  ) +
  guides(size = guide_legend("Power"), color = guide_colorbar("Age")) +
  labs(
    title = "",
    subtitle = "",
    caption = ""
  ) +
  scale_size_continuous(range = c(1,5)) +
  facet_grid(.~level)
## Coordinate system already present. Adding new coordinate system, which will replace the existing one.
ggplotly(plot02b, tooltip = "text")

Figure 4: The top 15-most powerful characters, with facet_grid(. ~ level)


Clearing

#clearing the environment
ls()
##  [1] "character_power" "db01a"           "db01b"           "db03"           
##  [5] "db04"            "db05"            "db06"            "db07"           
##  [9] "db08"            "db09"            "db11"            "law"            
## [13] "luffy00"         "op_ages"         "op_powers"       "plot01a"        
## [17] "plot01b"         "plot02a"         "plot02b"         "status"         
## [21] "villain_aff"
rm(list=ls())
 

Contact me at ap.sulistiono@gmail.com